rem INTRODUCING TWITCH BACKUP SUPREME!
rem
rem Proposed command line arguments: 1) Streamer name, 2) Video type, 3) Some videos back to --playlist-end, or all, 4) softcore or hardcore mode (ffmpeg repair and metadata)
rem
rem The first commands "@echo off" and "cls" mute unnecessary text in this script from displaying in this script/batch/.bat command window.
rem
@echo off
cls
rem
rem Below we use "pushd" to 1) guarantee that the current command line path is equal to the location of this .bat file (if you run .bat as admin, it changes the normal path), and 2) pushd creates a temporary drive mount if the current path is a network path, because .bat files don't run well from network paths. The "popd" at the end of the script unmounts the temporary drive from pushd at the start. 
rem
pushd "%~dp0"
rem
rem Below we set the %RootFolderName% variable to equal the current/root folder/directory holding this script. The user needs to manually name the root folder after the Twitch streamer's username. This is used to point the file downloader at the desired Twitch stream. We use two variableshere for readability and future maintainability.
rem
set FindRootFolder=%~p0
set FindRootFolder=%FindRootFolder:~0,-1%
for %%A in ("%FindRootFolder%") do (Set RootFolderName=%%~nxA)
set StreamerUsername=%RootFolderName%
rem 
rem Below we set the %DownloadFolderName% variable. The folder name is needed twice, so making it a variable in one place reduces the chance of human error editing this script in the future. We name the folder after the current script file name. We use two variables here for readability and future maintainability.
rem
set CurrentFileName=%~n0
set DownloadFolderName=%CurrentFileName%
rem
rem Below we name the command window, and print what it is doing.
rem
title Twitch_Backup_Supreme - %DownloadFolderName%
echo Running Twitch Backup Supreme ...
rem
rem To make maintenance easier, let's detect what this script is supposed to do based on its file name. That way we only need to have 1 script, just copied and renamed multiple times. We need to set TwitchLinkType and TwitchClipper variables. TwitchClipper should equal nothing unless the script file name is for clips, because the clips command is special, because it changes the file name format.
rem 
rem Putting quotes around a set variable is a trick to GUARANTEE that it's equal to nothing, with no invisible white space at the end.
set "TwitchClipper="
if "%DownloadFolderName%"=="Highlights_Backup" set "TwitchLinkType=videos?filter=highlights" && goto :PartyTime
if "%DownloadFolderName%"=="PastBroadcasts_Backup" set "TwitchLinkType=videos?filter=archives" && goto :PartyTime
if "%DownloadFolderName%"=="Uploads_Backup" set "TwitchLinkType=videos?filter=uploads" && goto :PartyTime
if "%DownloadFolderName%"=="Clips_Backup" set "TwitchLinkType=clips?filter=clips&range=all" && set "TwitchClipper=clip by %%(uploader)s"
:PartyTime

rem
rem Putting ".\" before a file/folder is a shortcut for the current path of where this script is. 
rem Below is an explanation of the youtube-dl command.
rem Option --ignore-config ignores the default youtube-dl config.txt in case there is one on your system.
rem Option --download-archive creates a file that remembers what files have been downloaded, so you can delete the files you don't want, and then youtube-dl won't try to download them again the next time you run this script.
rem Option --output sets the download folder and file name. The %% options pull Twitch text from youtube-dl. One "%" is used for manual command line execution, and two "%" are used for this script file execution because script files use one % for their own purposes.
rem Variable %RootFolderName% pulls the root folder name from the command at the start of the script, and uses this as the streamer's name on Twitch.
rem The Twitch link varies based on what you're trying to download. Find it on the streamer's Twitch Videos page.
rem
rem Running with --update first is important because Twitch changes things over time and breaks compatibility with old builds of youtube-dl.
rem The first command opens the sketchy youtubedl updater in a new window.
rem 
start /wait "Youtube-DL Updater" cmd /C "cls & echo Checking for update to Youtube-DL ... & ..\youtube-dl.exe --ignore-config --update & exit"
rem 
rem The below loop checks if the updater is still running and delays until it's done. We need to wait until the updater is FULLY done deleting the old youtube-dl.exe, and then it renames the new "youtube-dl.exe.new" file to youtube-dl.exe.
rem 
:UpdaterDelayLoop
tasklist /fi "windowtitle eq Youtube-DL*"  | find "===" >nul && goto :UpdaterDelayLoop
rem
rem Below we download the videos.
rem
start /B /wait "" ..\youtube-dl.exe --ignore-config --hls-prefer-ffmpeg --add-metadata --download-archive .\%DownloadFolderName%\download-archive.txt --output ".\%DownloadFolderName%\%%(upload_date)s - %%(view_count)s views - %%(title)s - %StreamerUsername% %TwitchClipper% (ID %%(id)s).%%(ext)s" "https://www.twitch.tv/%StreamerUsername%/%TwitchLinkType%"
IF ERRORLEVEL 1 (
..\youtube-dl.exe --ignore-config --download-archive .\%DownloadFolderName%\download-archive.txt --output ".\%DownloadFolderName%\%%(upload_date)s - %%(view_count)s views - %%(title)s - %StreamerUsername% %TwitchClipper% (ID %%(id)s).%%(ext)s" "https://www.twitch.tv/%StreamerUsername%/%TwitchLinkType%"
)
rem
rem Below the "popd" unmounts the temporary drive if one was created by pushd at the start.
rem
popd
rem
rem Below the final "pause" command keeps the .bat command window open after the work is done so you can see what happened. You can remove this pause if you want the window to close automatically.
rem
pause